﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using easyui.Models;
using GBLServices;
using Model.ViewModels;
using Microsoft.AspNetCore.Authorization;
using easyui.AuthHelper;

namespace easyui.Controllers
{
    public class HomeController : Controller
    {
        PermissionRequirement _permissionRequirement;
        MenuServices _Menu;
        public HomeController(PermissionRequirement permissionRequirement, MenuServices menu)
        {
            _Menu = menu;
            _permissionRequirement = permissionRequirement;
        }
        [AllowAnonymous]
        public IActionResult Index()
        {
            return View();
        }

        [HttpGet]
        [AllowAnonymous]
        [Route("/Home/GetMenu")]
        public JsonResult GetMenu()
        {
            var menus =_permissionRequirement.menus as List<MenuViewModel>;//_Menu.GetMenuAsync(1).Result;

            List<EasyuiMenu> easyuiMenus = new List<EasyuiMenu>();
            foreach (var item in menus.Where(x => x.module == 0))
            {
                EasyuiMenu easyuiMenu = new EasyuiMenu();
                easyuiMenu.id = item.code;
                easyuiMenu.state = TreeStates.closed;
                easyuiMenu.text = item.name;
                easyuiMenu.iconCls = "";
                easyuiMenu.attributes = "{\"url\":\"" + item.path + "\"}";
                easyuiMenu.@checked = false;
                easyuiMenu.children = getChildren(item.code, menus);

                easyuiMenus.Add(easyuiMenu);
            }
            return new JsonResult(easyuiMenus);
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }

        public List<EasyuiMenu> getChildren(string parentCode,List<MenuViewModel> menus)
        {
            List<EasyuiMenu> easyuiMenus = new List<EasyuiMenu>();
            foreach (var item in menus.Where(x => x.module.ToString() == parentCode))
            {
                EasyuiMenu easyuiMenu = new EasyuiMenu();
                easyuiMenu.id = item.code;
                easyuiMenu.state = TreeStates.closed;
                easyuiMenu.text = item.name;
                easyuiMenu.iconCls = "";
                easyuiMenu.attributes = "{\"url\":\"" + item.path + "\"}";
                easyuiMenu.@checked = false;
                easyuiMenu.children = getChildren(item.code, menus);
                if (easyuiMenu.children != null && easyuiMenu.children.Count == 0)
                {
                    easyuiMenu.children = null;
                }
                easyuiMenus.Add(easyuiMenu);
            }
            return easyuiMenus;
        }
    }
}
